<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb">	                        	
                        		<li>
		                        	<a  href="#"></a>
		                        </li>
		                        <li>
		                       		<a  href="#" ></a>
		                        </li>
		                        <li>
		                        	<a href="#"></a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal"  data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteId()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表展示-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                  </table>
			                  <!--数据列表/-->

                        </div>
                        <!-- 数据表格 /-->
						<input type="hidden" id="parentId">
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 增加 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td>
		      		   珠宝 >>  银饰
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  id="name" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>

							<select id="brand"  style="width: 100%"></select>

		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" onclick="save()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!--修改 -->
  <div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLal">商品分类编辑</h3>
		</div>
		<div class="modal-body">

			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td>
		      		   珠宝 >>  银饰
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>

		      		<td>
						<input type="hidden" id="id">
						<input type="hidden" id="parentId1">
						<input type="hidden" id="typeId">
						<input  id="name1" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>
		      	<tr>
		      		<td>类型模板</td>
		      		<td>

							<select id="brand1"  style="width: 100%"></select>

		      		</td>
		      	</tr>
			 </table>

		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" onclick="update()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
</body>
</html>
<script>
	$(function () {
		getData()
		getBrandsList()
	})
	function getData(id) {
		$.ajax({
			url:"/tbItemCatController/getInfo",
			dataType:"json",
			type:"post",
			data:{id : id},
			async : false,
			success:function (data) {

				var show = data.data
			if (data.code ==10000){
				var table = " <thead>";
				table += "<tr>";
				table += " <th class=\"\" style=\"padding-right:0px\">";
				table += " <input id=\"selall\" type=\"checkbox\"  class=\"icheckbox_square-blue\">";
				table += "</th>";
				table += "<th  class=\"sorting_asc\">分类ID</th>";
				table += "<th class=\"sorting\">分类名称</th>";
				table += "<th class=\"sorting\">类型模板ID</th>";
				table += "<th class=\"text-center\">操作</th>";
				table += "</tr>";
				table += " </thead>";
				for (var i = 0; i < show.length; i++) {
					table += "<tbody>";
					table += "<tr>";
					table += "<td>"
					table +="<input  type=\"checkbox\" value='"+show[i].id +"' name='check'> "
					table +="<input  type=\"hidden\" id =parentId value='"+show[i].parentId +"' > "
					table +="</td>"
					table += "<td> "+ show[i].id + "</td>";
					table += "<td>" + show[i].name + "</td>";
					table += "<td>" + show[i].typeId + "</td>";
					table += "<td class='text-center'> ";
					table += " <button type=\"button\" class=\"btn bg-olive btn-xs\" data-toggle=\"modal\"  data-target=\"#\" onclick=\"getData("+show[i].id+")\" >查看下级</button> ";
					table += " <button type=\"button\" class=\"btn bg-olive btn-xs\" data-toggle=\"modal\"  data-target=\"#edit\" onclick='toUpdate("+show[i].id+")'>修改</button> ";
					table += "</td>";
					table += "</tr>";
					table += "</tbody>";
				}
				$("#dataList").html(table);
			}else if (data.code==10001){
					table = "";
					table += "<input  type=\"hidden\" id =parentId value='" + data.data+ "' > "
				$("#dataList").html(table);
			}

			},
			error:function () {
				alert("网络异常")
			}
		})
	}


	function getBrandsList() {
		$.ajax({
			url: "/tbItemCatController/getShowId",
			type: "post",
			dataType: "json",
			async: true,
			success: function (result) {
				if (result.code == 10000) {
					var option = "";
					var list = result.data
					for (let i = 0; i < list.length; i++) {
						option +='<option value="'+list[i].id+'">';
						option +=list[i].name;
						option +='</option>';
					}
					$("#brand").html(option)

					$("#brand1").html(option)

				}

			},
			error: function () {
				alert("系统错误")
			}
		})
	}
	function save() {
		var name = 	$("#name").val();
		var typeId = $("#brand").val();
		var parentId =  $("#parentId").val();
		$.ajax({
			url: "/tbItemCatController/save",
			type: "post",
			data: {
				name :name,
				typeId :typeId ,
				parentId : parentId,
			},
			dataType: "json",
			async: true,
			success: function (result) {
				if (result.code == 10000) {
						alert("增加成功")
					location.reload()
					}
			},
			error: function () {
				alert("系统错误")
			}
		})

	}
	function toUpdate(id) {
		$.ajax({
			url: "/tbItemCatController/toUpdate?id="+id,
			type: "post",
			dataType: "json",
			async: true,
			success: function (result) {
				if (result.code == 10000) {
				 var list = 	result.data
					$("#name1").val(list.name)
					$("#brand1").val(list.typeId).trigger("change");
					$("#parentId").val(list.parentId)
					$("#id").val(list.id)
				}
			},
			error: function () {
				alert("系统错误")
			}
		})

	}
	function update() {
		$.ajax({
			url: "/tbItemCatController/update",
			type: "post",
			dataType: "json",
			data:{
				id: $("#id").val(),
				name: $("#name1").val() ,
					parentId: $("#parentId1").val(),
					typeId: $("#brand1").val()
			},
			async: true,
			success: function (result) {
				if (result.code == 10000) {
					alert("修改成功")
					location.reload()
				}
			},
			error: function () {
				alert("系统错误")
			}
		})
	}
	function deleteId(){
		var ids = [];
		$("[name=check]").each(function (i,e) {
			if ($(e).prop("checked")){
				ids.push($(e).val())
			}
		})

		$.ajax({
			url : "/tbItemCatController/deleteAll",
			type : "post",
			data: {ids : ids},
			dataType : "json",
			async : true,
			success : function (result) {
				if (result.code ==10000){
					alert(result.message)
				}
				window.location.reload();

			},
			error : function () {
				alert("网络异常");
			}

		})
	}



</script>